/*
 * SubmitOrderSuccessPresenter.java
 * business
 *
 * Created by chentao on 2017/5/9.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.multipayways;

import android.os.Bundle;
import android.text.TextUtils;

import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.PayWay;
import com.yidiandao.business.data.model.IConfirmOrderModel;
import com.yidiandao.business.data.response.QueryMultiPayWaysResultResponse;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 提交订单成功Presenter
 */
public class MultiPayWaysPresenter implements MultiPayWaysContract.Presenter {

    private MultiPayWaysContract.View mSubmitOrderSuccessView;

    private IConfirmOrderModel mConfirmOrderModel;

    private String mOrderPayId;

    private boolean mRefreshData;

    public MultiPayWaysPresenter(MultiPayWaysContract.View view, IConfirmOrderModel confirmOrderModel, Bundle bundle) {
        mSubmitOrderSuccessView = view;
        mSubmitOrderSuccessView.setPresenter(this);

        mConfirmOrderModel = confirmOrderModel;
        mOrderPayId = bundle.getString(Intents.EXTRA_ORDER_PAY_ID);
    }

    @Override
    public void start() {
        mConfirmOrderModel.loadMultiPayWaysResult(mSubmitOrderSuccessView.getPagerTag(), mOrderPayId, new IConfirmOrderModel.OnMultiPayWayResultListener() {
            @Override
            public void onSuccess(QueryMultiPayWaysResultResponse.Data data) {

                mSubmitOrderSuccessView.refreshContent(SuperLoadingLayout.STATE_SUCCESS);

                List<Object> items = new ArrayList<>();
                List<QueryMultiPayWaysResultResponse.PayWayState> payWaysState = data.getPayWayState();
                QueryMultiPayWaysResultResponse.OnlinePayState onlinePayState = null;

                for (QueryMultiPayWaysResultResponse.PayWayState state : payWaysState) {
                    if (state.getPayCode().equals(PayWay.Flavor.ONLINE)) {
                        // 在线支付
                        if (!TextUtils.isEmpty(data.getUrl())) {
                            // 已经支付了 --> 不显示在线支付的按钮
                            // 未支付 --> 显示在线支付的按钮
                            onlinePayState = new QueryMultiPayWaysResultResponse.OnlinePayState();
                            onlinePayState.setMoney(state.getMoney());
                            onlinePayState.setUrl(data.getUrl());
                            onlinePayState.setOrderPayId(mOrderPayId);
                        }
                        break;
                    }
                }

                items.addAll(payWaysState);

                // 根据在线支付是否已经支付来显示或隐藏支付按钮
                if (onlinePayState != null) {
                    items.add(onlinePayState);
                }

                mSubmitOrderSuccessView.setItems(items);
            }

            @Override
            public void onFailed() {
                mSubmitOrderSuccessView.refreshContent(SuperLoadingLayout.STATE_ERROR);
            }
        });
    }

    @Override
    public void toOrderResultActivity() {
        mSubmitOrderSuccessView.toOrderResultActivity(mOrderPayId);
    }

    @Override
    public void needRefreshData() {
        mRefreshData = true;
    }

    @Override
    public void checkRefreshData() {
        if (mRefreshData) {
            start();
        }
    }
}
